/////////////////////////////////////////////////////////
//                                                     //
// Copyright (c) 2016年 上证云平台. All rights reserved. //
//                                                     //
/////////////////////////////////////////////////////////


#import "ResponseViewController.h"
#import "MApi.h"
#import "NSObject+Description.h"
@interface ResponseViewController ()
@property (strong, nonatomic) UITextView *textView;
@end

@implementation ResponseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIButton *but = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    [but addTarget:self action:@selector(noticeAction) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:but];
    [self.view addSubview:self.textView];
    
}
-(void)noticeAction
{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"1，如果您在使用过程中发现部分接口没有数据，可以自己在配置文件Level-1.json或者Level-2.json中更改相应的请求参数。\n2，行情类接口stockItem实例中部分字段没有数据可能是该权限下或者该接口不返回此字段数据的原因。\n3，如有任何疑问可以随时和我们的相关人员联系。" delegate:self cancelButtonTitle:@"返回" otherButtonTitles:nil, nil];
    [alert show];
}
-(UITextView *)textView
{
    if (!_textView){
        _textView = [[UITextView alloc]initWithFrame:self.view.frame];
        _textView.backgroundColor = [UIColor whiteColor];
        _textView.editable = NO;
        _textView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    }
    return _textView;
}
- (void)viewWillAppear:(BOOL)animated {
    NSDictionary *parmInfo = self.info[@"param"];
    Class class = NSClassFromString(self.info[@"request"]);
    MRequest *request = [[class alloc]init];
    for (NSDictionary *dict in parmInfo) {
        NSString *type = dict[@"type"];
        id value = dict [@"value"];
        if ([type isEqualToString:@"MChartType"]) {
            value = [NSNumber numberWithInt:[self formateChartTypeString:value]];
        }
        else if ([type isEqualToString:@"MOHLCPeriod"]) {
            value = [NSNumber numberWithInt:[self formateMOHLCPeriodString:value]];
        } else if ([type isEqualToString:@"MOptionType"]) {
            value = [NSNumber numberWithInt:[self formateOptionTypeString:value]];
        } else if ([type isEqualToString:@"NSInteger"]) {
            value = [NSNumber numberWithInt:[value intValue]];
        } else if ([type isEqualToString:@"MF10RequestType"]) {
            value = [self F10RequestTypeString:value];
        }
        
        [request setValue:[self formateExternString:value] forKey:dict[@"key"]];
    }
    
    [MApi sendRequest:request completionHandler:^(MResponse *resp) {
        if(resp.status == MResponseStatusSuccess) {
            NSDictionary *des = [resp mapi_description:resp.class];
            NSString *str = [NSString stringWithCString:[des.description cStringUsingEncoding:NSUTF8StringEncoding] encoding:NSNonLossyASCIIStringEncoding];
            if (str) {
                self.textView.text = str;
            }else {
                self.textView.text = des.description;
            }
        } else {
            //应答错误，显示错误信息
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Error" message:resp.message delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert show];
            NSLog(@"%@",resp.message);
        }
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - formate method

- (int)formateChartTypeString:(id)chartTypeString {
    NSDictionary *chartTypeDict = [NSDictionary dictionaryWithObjectsAndKeys:
                                   [NSNumber numberWithInteger:MChartTypeOneDay],@"MChartTypeOneDay",
                                   [NSNumber numberWithInteger:MChartTypeFiveDays],@"MChartTypeFiveDays",nil];
    
    return [[chartTypeDict objectForKey:chartTypeString] intValue];
}

- (int)formateMOHLCPeriodString:(id)ohlcPeriodString {
    NSDictionary *ohlcDict = [NSDictionary dictionaryWithObjectsAndKeys:
                              [NSNumber numberWithInteger:MOHLCPeriodDay],@"MOHLCPeriodDay",
                              [NSNumber numberWithInteger:MOHLCPeriodWeek],@"MOHLCPeriodWeek",
                              [NSNumber numberWithInteger:MOHLCPeriodMonth],@"MOHLCPeriodMonth",
                              [NSNumber numberWithInteger:MOHLCPeriodMin15],@"MOHLCPeriodMin15",nil];
    
    return [[ohlcDict objectForKey:ohlcPeriodString] intValue];
}

- (int)formateOptionTypeString:(id)optionTypeString {
    NSDictionary *optionDict = [NSDictionary dictionaryWithObjectsAndKeys:
                                [NSNumber numberWithInteger:MOptionTypeUnknown],@"MOptionTypeUnknown",
                                [NSNumber numberWithInteger:MOptionTypePut],@"MOptionTypePut",
                                [NSNumber numberWithInteger:MOptionTypeCall],@"MOptionTypeCall",nil];
    
    return [[optionDict objectForKey:optionTypeString] intValue];
}

- (NSString *)formateExternString:(NSString *)string
{
    NSDictionary *dic = @{@"MNewsTypeImportant":MNewsTypeImportant,
                          @"MNewsTypeRoll":MNewsTypeRoll,
                          @"MNewsTypeFinance":MNewsTypeFinance,
                          @"MNewsTypeIndustry":MNewsTypeIndustry,
                          @"MNewsTypeStock":MNewsTypeStock,
                          @"MNewsTypeFuture":MNewsTypeFuture,
                          @"MNewsTypeForeignExchange":MNewsTypeForeignExchange,
                          @"MNewsTypeFund":MNewsTypeFund,
                          @"MNewsTypeBond":MNewsTypeBond,
                          @"MNewsTypeGold":MNewsTypeGold,
                          @"MNewsTypeOthers":MNewsTypeOthers,
                          @"MGradeFundTypeInfo":MGradeFundTypeInfo,
                          @"MGradeFundTypePosition":MGradeFundTypePosition,
                          @"MGradeFundTypeDiagnose":MGradeFundTypeDiagnose,
                          @"MGradeFundTypeSubscribe":MGradeFundTypeSubscribe,
                          @"MGradeFundTypeSplit":MGradeFundTypeSplit,
                          @"MGradeFundTypeConvert":MGradeFundTypeConvert,
                          @"MGradeFundTypeParentFund":MGradeFundTypeParentFund};
    return dic[string]?:string;
}
- (NSString *)F10RequestTypeString:(NSString *)string {
    NSDictionary *dic = @{@"MF10RequestTypeImportantIndex":@"importantindex",\
                          @"MF10RequestTypeBusinessInfo":@"businessinfo",\
                          @"MF10RequestTypeCharts5Buys":@"charts5buys",\
                          @"MF10RequestTypeCharts5Sells":@"charts5sells",\
                          @"MF10RequestTypeExptskinvrating":@"exptskinvrating",\
                          @"MF10RequestTypeExptskstatn":@"exptskstatn",\
                          @"MF10RequestTypeCompanyInfo":@"companyinfo",\
                          @"MF10RequestTypeShareBonus":@"sharebonus",\
                          @"MF10RequestTypeCompanyManager":@"companymanager",\
                          @"MF10RequestTypeTradableShareholders10":@"otsholder10",\
                          @"MF10RequestTypeShareholder10":@"shareholder10",\
                          @"MF10RequestTypeShareInfo":@"shareinfo",\
                          @"MF10RequestTypeShareholderNumber":@"shareholdernum",\
                          @"MF10RequestTypeMainIndex":@"profinmainindex",\
                          @"MF10RequestTypeProfitStatement":@"proincstatementnew",\
                          @"MF10RequestTypeBalanceSheet":@"probalsheetnew",\
                          @"MF10RequestTypeCashFlowStatement":@"procfstatementnew",\
                          @"MF10RequestTypeBlockTrade":@"blocktrade",\
                          @"MF10RequestTypeTradingDetail":@"tradingdetail",\
                          @"MF10RequestTypeIinvholdchg":@"iinvholdchg",\
                          @"MF10RequestTypeImportnoticedate":@"importnoticedate",\
                          @"MF10RequestTypeImportnoticetitle":@"importnoticetitle"};
    return dic[string]?:string;
}
@end
